package com.astro.app;

import java.io.File;
import java.io.FileInputStream;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.astro.business.UserManager;
import com.astro.common.Constants;
import com.astro.domain.bean.TblUserReport;

public class ImportPdf {
	
	static final Logger log = Logger.getLogger(ImportPdf.class);
	
	public static void main(String[] args) {
		System.out.println("Program is starting...");
		try {
			// Initial application context
			System.out.println(" spring context is loading...");
			AppContext.setSpringContext(new ClassPathXmlApplicationContext(
					"spring-datasource-config.xml"));
			System.out.println(" spring context is loaded!");

			UserManager userManager = (UserManager) AppContext
					.getSpringContext().getBean("userManager");
			
			AppConfig appConfig = (AppConfig) AppContext.getSpringContext().getBean(AppConfig.class);
			String email = "test@test.my";
			String path = appConfig.getProp(Constants.PDF_PATH_LAOD2DB);
			
			File folder = new File(path);
			TblUserReport userReport = null;
			if (folder.isDirectory()) {
				File[] files = folder.listFiles();
				if (files != null && files.length >= 0) {
					for (int i = 0; i < files.length; i++) {
						
			            FileInputStream inputStream = new FileInputStream(files[i]);
			            int fileSize = (int)files[i].length();
			            byte[] reportBLOB = new byte[fileSize];
			            inputStream.read(reportBLOB);
			            
			            userReport = new TblUserReport();
						userReport.setEmail(email);
						userReport.setReportBLOB(reportBLOB);
						userManager.addUserReport(userReport);
					}
				}
			}
				
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("Done!");
	}
}
